package com.ssy.lingxi.member.merchant.model.bo;

import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.*;

/**
 * 角色、会员、用户的权限配置BO对象，对应数据库jsonb字段
 * @author 万宁
 * @version 2.0.0
 * @date 2020-06-24
 */
public class AuthBO implements Serializable{
    private static final long serialVersionUID = -2862592515619439514L;

    public AuthBO() {}

    public AuthBO(Long id, Long parentId, String code, String url, String title, Integer order, Integer source, Map<String, Object> attrs, List<ButtonBO> buttons, Integer status) {
        this.id = id;
        this.parentId = parentId;
        this.code = code;
        this.url = url;
        this.title = title;
        this.order = order;
        this.source = source;
        this.attrs = attrs == null ? new HashMap<>() : attrs;
        this.buttons = buttons == null ? new ArrayList<>() : buttons;
        this.status = status;
    }

    /**
     * 权限菜单的id
     */
    private Long id;

    /**
     * 父菜单Id
     */
    private Long parentId;

    /**
     * 权限菜单的code
     */
    private String code;

    /**
     * 权限菜单的url
     */
    private String url;

    /**
     * 权限菜单名称
     */
    private String title;

    /**
     * 顺序
     */
    private Integer order;

    /**
     * 菜单适用的平台
     */
    private Integer source;

    /**
     * 菜单属性
     */
    private Map<String, Object> attrs;

    /**
     * 菜单关联的按钮
     */
    private List<ButtonBO> buttons;

    /**
     * 是否显示（App客户端用户可以展示或隐藏菜单）
     */
    private Integer status;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getParentId() {
        return parentId;
    }

    public void setParentId(Long parentId) {
        this.parentId = parentId;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Integer getOrder() {
        return order;
    }

    public void setOrder(Integer order) {
        this.order = order;
    }

    public Integer getSource() {
        return source;
    }

    public void setSource(Integer source) {
        this.source = source;
    }

    public Map<String, Object> getAttrs() {
        return attrs;
    }

    public void setAttrs(Map<String, Object> attrs) {
        this.attrs = attrs;
    }

    public List<ButtonBO> getButtons() {
        return buttons;
    }

    public void setButtons(List<ButtonBO> buttons) {
        this.buttons = buttons;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }

        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        AuthBO authBO = (AuthBO) o;
        //判断按钮是否相等： “按钮都为空” 或 “所有按钮的Id都相同”
        boolean bothEmpty = CollectionUtils.isEmpty(buttons) && CollectionUtils.isEmpty(authBO.getButtons());
        boolean allButtonEquals = buttons.stream().allMatch(b -> authBO.buttons.stream().anyMatch(a -> a.equals(b))) && authBO.getButtons().stream().allMatch(a -> buttons.stream().anyMatch(b -> b.equals(a)));
        return id.equals(authBO.id) && (bothEmpty || allButtonEquals);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }
}
